package com.ny.demo525.impl;
import com.ny.connect.SqlConnection;
import com.ny.entity.Student;
import com.ny.demo525.StudentDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDaolmpl  implements StudentDao {

    private Connection connection;

    public StudentDaolmpl() throws SQLException, ClassNotFoundException {
    }

    @Override
    public int add(Student student) throws SQLException {
        String sql = "insert into students(name,password,phone)  values(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, student.getStudentName());
        preparedStatement.setString(2, student.getStudentName());
        preparedStatement.setString(3, student.getStudentName());
        return preparedStatement.executeUpdate();
    }

    @Override
    public Student login(String Studentname, String studentPassword) throws SQLException, ClassNotFoundException {
        String sql = "select * from students where name = ? and password = ?";
        Connection connection = SqlConnection.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, Studentname);
        preparedStatement.setString(2, studentPassword);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            Student student = new Student();
            student.setStudentId(resultSet.getInt("student_id"));
            student.setStudentName(resultSet.getString("name"));
            student.setStudentName(resultSet.getString("phone"));
            student.setStudentPassword(resultSet.getString("password"));
            return student;
        }
        return null;
    }
}


